package chapter6.meine.GUIexamples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import chapter5.v2.Card;
import chapter5.v2.Deck;
import chapter5.v2.Hand;
import chapter6.SimpleCalc.CalcPanel;
import chapter6.meine.GUIexamples.FourActionsCalc.MainPanel;

public class GuessCardGUIGame extends JApplet {

	public static void main(String[] args) {
		JFrame window = new JFrame("Simple Calculator");
		MainPanel panel = new MainPanel();
		window.setContentPane(panel);
		window.pack(); // Sizes window to preferred size of contents.
		window.setLocation(100, 100);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}// end of main()

	public void init() {
		setContentPane(new MainPanel());
	}// end of init()

	public static class MainPanel extends JPanel {

		private JButton buttHigh, buttLow, buttNewGame;
		private JPanel panelCenter, panelBottom;
		private JLabel lblMessage;
		private String message;
		private boolean isGameIsGoing = true;

		public MainPanel() {
			setLayout(new BorderLayout(3, 3));
			setBackground(new Color(10,180,40) );
			setBorder(BorderFactory.createLineBorder(Color.gray, 3));

			CardPanel cardPanel = new CardPanel();
			cardPanel.setLayout(new BorderLayout());
			cardPanel.setForeground(Color.cyan);
			cardPanel.setOpaque(false);
			add(cardPanel);

			add(cardPanel, BorderLayout.CENTER);

			panelBottom = new JPanel();
			add(panelBottom, BorderLayout.SOUTH);

			buttHigh = new JButton("Higher");
			buttHigh.addActionListener(cardPanel);
			panelBottom.add(buttHigh);

			buttLow = new JButton("Lower");
			buttLow.addActionListener(cardPanel);
			panelBottom.add(buttLow);

			buttNewGame = new JButton("New Game!");
			buttNewGame.addActionListener(cardPanel);
			panelBottom.add(buttNewGame);

		} // end of MainPanel()

		private class CardPanel extends JPanel implements ActionListener {

			private Card card;
			private Deck deck;
			private Hand hand;
			private int xCor, yCor, cardWidth, cardHeight, cardCount;
			private boolean isGameIsGoing;

			CardPanel() {
				setPreferredSize(new Dimension(400, 150));
				startNewGame();
			}

			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand() == "Higher") {
					compareHigher();
				} else if (e.getActionCommand() == "Lower") {
					compareLower();
				} else if (e.getActionCommand() == "New Game!") {
					startNewGame();
				}

			}// end of actionPerformed()

			void startNewGame() {
				if (isGameIsGoing) {
					message = "Finish this game first!";
					repaint();
					return;
				}

				deck = new Deck();
				card = new Card();
				hand = new Hand();
				deck.shuffle();
				card = deck.dealCard();
				hand.addCard(card);
				message = "Is next Card higher or Lower?";
				isGameIsGoing = true;
				repaint();
			}// end of startNewGame()

			void compareLower() {
				if (isGameIsGoing == false) {
					message = "Click button \"New Game\" to start a new game";
					repaint();
					return;
				}

				Card thisCard = deck.dealCard();
				hand.addCard(thisCard);
				cardCount = hand.getCardCount();
				Card prevCard = hand.getCard(hand.getCardCount() - 2);

				if (prevCard.getValue() > thisCard.getValue()) {
					message = "You win, try for " + cardCount + 1+" !";
				} else if (prevCard.getValue() < thisCard.getValue()) {
					isGameIsGoing = false;
					message = "You loose, Card is higher! Try Next Game!";

				} else if (prevCard.getValue() == thisCard.getValue()) {
					isGameIsGoing = false;
					message = "You loose on ties! Try Next Game!";

				}
				else if (cardCount == 4) {
					isGameIsGoing = false;
					message = "You WIN! You've predicted correctly for 3 times in a row. Play again?";
				}
				repaint();
			}

			void compareHigher() {
				if (isGameIsGoing == false) {
					message = "Click button \"New Game\" to start a new game";
					repaint();
					return;
				}

				Card thisCard = deck.dealCard();
				hand.addCard(thisCard);
				cardCount = hand.getCardCount();
				Card prevCard = hand.getCard(hand.getCardCount() - 2);

				if (prevCard.getValue() < thisCard.getValue()) {
					message = "You win, try for" + cardCount + "!";
				} else if (prevCard.getValue() > thisCard.getValue()) {
					isGameIsGoing = false;
					message = "You loose, Card is lower! Try Next Game!";

				} else if (prevCard.getValue() == thisCard.getValue()) {
					isGameIsGoing = false;
					message = "You loose on ties! Try Next Game!";

				}
				else if (cardCount == 4) {
					isGameIsGoing = false;
					message = "You WIN! You've predicted correctly for 3 times in a row. Play again?";
				}
				repaint();
			}

			public void paintComponent(Graphics g) {

				xCor = 20;
				yCor = 20;
				cardWidth = getWidth() / 5;
				cardHeight = getHeight() / 2;
				cardCount = hand.getCardCount();
				for (int i = 0; i < cardCount; i++) {
					drawCard(hand.getCard(i), g, xCor + i * (20 + cardWidth),
							yCor);
				}
				if (isGameIsGoing)
					drawCard(null, g, xCor + cardCount * (20 + cardWidth), yCor);

			}// end of compareLower()

			public void drawCard(Card card, Graphics g, int x, int y) {
				if (card == null) {
					g.setColor(Color.blue);
					g.fillRect(x, y, cardWidth, cardHeight);
					g.fillRect(x + 4, y + 4, cardWidth - 8, cardHeight - 8);
					g.setColor(Color.white);
					g.drawRect(x + 2, y + 2, cardWidth - 4, cardHeight - 4);
				} else {
					g.setColor(Color.gray);
					g.fillRect(x, y, cardWidth, cardHeight);

					g.setColor(Color.white);
					g.fillRect(x + 2, y + 2, cardWidth - 4, cardHeight - 4);

					g.setColor(Color.black);
					g.drawString(card.getValueAsString(), x + 10, y + 20);
					g.drawString(" of ", x + 10, y + 40);
					g.drawString(card.getSuitAsString(), x + 10, y + 60);
					g.drawString(message, 20, cardHeight + 50);
				}

			}

		}// end of class CardPanel

	} // end of class MainPanel

}
